#include "../../../libraries/sockets/tcp_server.h"
#include "../../../libraries/operations/operations.h"
#include <iostream>
#include <sstream>
#include <string>
#include "../../../config.h"

#include <cstdlib>


using namespace std; 

int main(int argc, char *argv[])

{
    if (argc<2)
    {
        cout <<"use this format: ./server portno" <<endl;
        exit(1);
    }

    TCP_server server(atoi (argv[1]), 20);  //Creamos socket

    while (1){

        if(server.accept()==0) 
        {
    
	    enum oper{Sm, Rs, Dv, Mp, Ex, Rz };
	    char ope[2];
	    string op, summ1, summ2;
	    int Sum1=0, Sum2=0, result=0;
	 
	    server >> op;
	    server << "Enter one number: \n";
	    server >> summ1;

	    Sum1=atoi(summ1.c_str());
	    

	    if(atoi(op.c_str()) != Rz)
	    {
		server<< "Enter another number: \n";
		server >> summ2;
		Sum2=atoi(summ2.c_str());
	    }



	    switch (atoi(op.c_str()))
	    {
		    case Sm:
			result=suma(Sum1,Sum2);
			break;

		    case Rs:
			result=resta(Sum1,Sum2);
			break;

		    case Dv:
			result=divi(Sum1,Sum2);
			break;

		    case Mp:
			result=mult(Sum1,Sum2);
			break;

		    case Ex:
			result=exp(Sum1,Sum2);
			break;

		    case Rz:
			result=raiz(Sum1);
			break;

		    default:

			cout << "Operación no reconocida\n" << endl;
			break;
	    }//end switch


	    string result_str; //Convert int to string

	    stringstream result_ss;
	    result_ss << result;
	    result_str = result_ss.str();
	    server << result_str;
	    exit(0);

	}


    }
    return 0;  
}
